﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Xml.Linq;

namespace HeroldIT.Dnn.Modules.News
{
    public class NewsEntryDetailCollection : KeyedCollection<string, NewsEntryDetail>
    {
        #region fields

        private readonly NewsEntry owner;

        #endregion

        #region properties

        public new NewsEntryDetail this[string locale]
        {
            get
            {
                if (!this.Contains(locale))
                {
                    NewsEntryDetail detail = new NewsEntryDetail {Locale = locale};
                    this.OnCreatingNewsEntryDetail(new EventArgs<NewsEntryDetail>(detail));
                    this.Add(detail);
                }
                return base[locale];
            }
            set
            {
                if (this.Contains(locale))
                {
                    this.Remove(locale);
                }
                value.Locale = locale;
                this.Add(value);
            }
        }

        #endregion

        #region events

        public event EventHandler<EventArgs<NewsEntryDetail>> CreatingNewsEntryDetail;

        protected virtual void OnCreatingNewsEntryDetail(EventArgs<NewsEntryDetail> e)
        {
            EventHandler<EventArgs<NewsEntryDetail>> handler = this.CreatingNewsEntryDetail;
            if (handler != null) handler(this, e);
        }

        #endregion

        #region constructors

        internal NewsEntryDetailCollection(NewsEntry owner)
        {
            this.owner = owner;
        }

        #endregion

        #region methods

        protected override string GetKeyForItem(NewsEntryDetail item)
        {
            return item.Locale;
        }

        protected override void InsertItem(int index, NewsEntryDetail item)
        {
            item.NewsEntryId = owner.NewsEntryId;
            base.InsertItem(index, item);
        }

        protected override void SetItem(int index, NewsEntryDetail item)
        {
            item.NewsEntryId = owner.NewsEntryId;
            base.SetItem(index, item);
        }

        public NewsEntryDetail GetDefaultDetail()
        {
            return this.GetDefaultDetail(null);
        }

        public NewsEntryDetail GetDefaultDetail(string defaultLocale)
        {
            NewsEntryDetail result = this.Where(d => d.IsDefaultLocale).FirstOrDefault();
            if (null == result && !String.IsNullOrEmpty(defaultLocale))
            {
                result = this[defaultLocale];
                if (result.IsEmpty())
                {
                    result = null;
                }
            }
            if (null == result && this.Count > 0)
            {
                result = this[0];
            }
            return result;
        }

        public void Import(XElement toImport)
        {
            if (null == toImport)
                return;

            if (toImport.Name != "details")
                throw new ArgumentException("The element to import is not a valid details export.", "toImport");

            toImport.Elements("detail")
                .All(e =>
                         {
                             NewsEntryDetail detail = NewsEntryDetail.Import(e);
                             if (null != detail)
                             {
                                 this[detail.Locale] = detail;
                             }
                             return true;
                         });
        }

        public XElement Export()
        {
            if (this.Count <= 0)
                return null;

            return new XElement("details",
                    this.Select(e => e.Export())
                );
        }

        #endregion
    }
}
